package svc

import (
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/common/gorseclient"
	"mall/service/backend/rmq/internal/config"
	"mall/service/backend/rpc/backendclient"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/rpc/forumclient"
	"mall/service/order/rpc/orderclient"
	"mall/service/product/rpc/productclient"
	"mall/service/promotion/rpc/promotionclient"
	"mall/service/setting/rpc/settingclient"
	"mall/service/user/rpc/userclient"

	"github.com/elastic/go-elasticsearch/v7"
	"github.com/silenceper/wechat/v2/miniprogram"
	"github.com/zeromicro/go-zero/core/stores/redis"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config config.Config

	MiniProgram *miniprogram.MiniProgram
	EsClient    *elasticsearch.Client
	GorseClient gorseclient.GorseClient
	BizRedis    *redis.Redis

	AttachmentRpc attachmentclient.Attachment
	BackendRpc    backendclient.Backend
	BlogRpc       blogclient.Blog
	ForumRpc      forumclient.Forum
	OrderRpc      orderclient.Order
	ProductRpc    productclient.Product
	PromotionRpc  promotionclient.Promotion
	UserRpc       userclient.User
	SettingRpc    settingclient.Setting
}

func NewServiceContext(c config.Config) *ServiceContext {
	esClient, _ := newElasticClient(c)

	rds := redis.MustNewRedis(c.BizRedis)

	return &ServiceContext{
		Config: c,

		MiniProgram: newMiniprogramClient(c),
		EsClient:    esClient,
		GorseClient: gorseclient.NewGorseClient(),
		BizRedis:    rds,

		AttachmentRpc: attachmentclient.NewAttachment(zrpc.MustNewClient(c.AttachmentRpc)),
		BackendRpc:    backendclient.NewBackend(zrpc.MustNewClient(c.BackendRpc)),
		BlogRpc:       blogclient.NewBlog(zrpc.MustNewClient(c.BlogRpc)),
		ForumRpc:      forumclient.NewForum(zrpc.MustNewClient(c.ForumRpc)),
		OrderRpc:      orderclient.NewOrder(zrpc.MustNewClient(c.OrderRpc)),
		ProductRpc:    productclient.NewProduct(zrpc.MustNewClient(c.ProductRpc)),
		PromotionRpc:  promotionclient.NewPromotion(zrpc.MustNewClient(c.PromotionRpc)),
		UserRpc:       userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
		SettingRpc:    settingclient.NewSetting(zrpc.MustNewClient(c.SettingRpc)),
	}
}
